<?php
class Merchant_Service_ProductProccess
{
	protected $_product = null;	 
	protected $_productId = null;
	protected $_plan = null;
	protected $_customerId = null;
	protected $_procCnt  = 0;
	protected $_products = null;
	protected $_chargeBalance = 0;//扣取费用
	 
	protected $_chargeBalanceCurrency = null;
	 
	protected $_outOfBalance = false;
	protected $_error = null;
	protected $_warning = null;
	protected $_customer = null;
	protected $_logNote = null;
	protected $_unactive = false;
	
	protected $_status = null;
	protected $_checkOutOfPlan  = false;
	protected $_customerPlan = null;
	protected $_gwem = false;//注册在一个月内
	public function __construct($productId = null,$customerId=null){	
		 if(isset($productId)){
			$this->_productId = $productId;
			$this->setCustomer($customerId);
			$this->setProduct();
			 
		}
	}
	/**
	*注册日期在一个月内
	*/
	 private function getByCustomerIdGwem(){
		$CustomerModel= new Merchant_Model_Customer();
		$cRow=$CustomerModel->getByGwemCustomerId($this->_customerId);
		if(!empty($cRow)){
			$this->_gwem=true;
		}
	 }
	/**
	* 用户分发订单
	* @param orderCode
	* @return array|boolean success return boolean,false return array
	*/
	 public function submited($status,$productId=null,$customerId=null){
		if(isset($productId)){
			$this->_productId = $productId;
			$this->setCustomer($customerId);
			$this->setProduct();			
		}
		
		if($this->_error!=null){
			return array('product_sku'=>'','msg'=>$this->_error,'ask'=>'Error');	
		}
		$this->_status = $status;
		$productService= new Merchant_Service_Product();
		$productModel= new Merchant_Model_Product();
		if($this->_product['product_status'] == $status){
 
			return array('product_sku'=>$this->_product['product_sku'],'msg'=>'Success.','ask'=>'Success');	
		}
		switch($status){
			case 0:
				
				return $this->delete();
				break;
			case 1://激活产品
				
				return $this->active();
				break;
			case 2://取消激活
				
				return $this->unActive();
				break;
				
		}
	 }
	 /*
	 *激活产品
	 *
	 */
	 private function active(){
		if($this->_product['product_status'] != '2'){
			return array('product_sku'=>$this->_product['product_sku'],'msg'=>'Failure,the product status must be draft.','ask'=>'Error');	
		}
		$this->_procCnt = 1;
		$this->_logNote = "Active.";		

		$customerBalance = new Merchant_Model_CustomerBalance();
		$dbAdapter = $customerBalance->getAdapter();		 
		$dbAdapter->beginTransaction();
		try{
			$this->update();			
			$dbAdapter->commit();
			return array('product_sku'=>$this->_product['product_sku'],'msg'=>"Success",'ask'=>'Success');	
		}catch(Exception $e){
			$dbAdapter->rollback();
			return array('product_sku'=>$this->_product['product_sku'],'msg'=>'Internal Error','ask'=>'Error');		
		}				 
	 }
	 /*
	 *下架已激活产品
	 *
	 */
	 private function unActive(){
		if($this->_product['product_status'] != 1){
			return array('product_sku'=>$this->_product['product_sku'],'msg'=>'Failure,the product status must be activated','ask'=>'Error');
		}
		$this->_procCnt = -1;
		$this->_logNote = "Unactive.";
		
		
		$this->checkUnactive();
		if($this->_unactive){
			return array('product_sku'=>$this->_product['product_sku'],'msg'=>$this->_error,'ask'=>'Error');	
		}

		$customerBalance = new Merchant_Model_CustomerBalance();
		$dbAdapter = $customerBalance->getAdapter();		 
		$dbAdapter->beginTransaction();
		try{

			$this->update();
			
			$dbAdapter->commit();
			return array('product_sku'=>$this->_product['product_sku'],'msg'=>"Success",'ask'=>'Success');	
		}catch(Exception $e){
			$dbAdapter->rollback();
			return array('product_sku'=>$this->_product['product_sku'],'msg'=>'Internal Error','ask'=>'Error');		
		}
	 
	 }
	 
	 public function delete(){
		if($this->_product['product_status'] != 2 || $this->_product['product_warehouse_status'] != 0){
				// $dbAdapter->rollback();
				return array('product_sku'=>$this->_product['product_sku'],'msg'=>'Failure, "Delete product" the product status  must be draft and never receiving','ask'=>'Error');	
			}
		$this->update();
		return array('product_sku'=>$this->_product['product_sku'],'msg'=>"Success",'ask'=>'Success');	
	 }
	 /*
	 *
	 *获取客户LOGIN信息
	 *
	 */
	 private function setCustomer($customerId=null){
		if(!$customerId){
			$sessionUser = new Zend_Session_Namespace("customer");
			$this->_customer = $sessionUser->customer;
			$this->_customerId = $sessionUser->customer['customer_id'];
		}else{
			$this->_customerId = $customerId;
		}
	 }
	

	  /*
	 *
	 *Check unactive
	 */
	 private function checkUnactive(){ //Todo
			$this->_unactive = false;
			//判断库存
			$wpObj = new Merchant_Model_WarehouseProducts();
			$condition['product_id'] = $this->_productId;
			$warehouseProduct = $wpObj->getByCondition($condition);
			if($warehouseProduct&&!empty($warehouseProduct)){
				foreach ($warehouseProduct as $wp){
					if($wp['wp_quantity_hold'] > 0 || $wp['wp_quantity']>0){
						$this->_unactive =true;
						$this->_error = "Have available inventory, can not be unactive.";
						break;
					}
				}
			}
			

			//判断是否存在末发货订单			
			if(!$this->_unactive){
				$orderObj = new Merchant_Model_Orders();
				$quantity = $orderObj->getByProductId($this->_productId);
				if($quantity > 0){
					$this->_unactive =true;
					$this->_error = "Have order, can not be unactive.";
				}
			}
			
			//判断是否存在末完成的ASN
			if(!$this->_unactive){
				$asnObj = new Merchant_Model_Receiving();
				$quantity = $asnObj->getByProductId($this->_productId);
				if($quantity > 0){
					$this->_unactive =true;
					$this->_error = "Have asn, can not be unactive.";
				}
			}
			
	 }
	 
	 
	 
	 //TODO validate Product Info//
	 private function vaildateProduct(){
		$productObj = new Merchant_Service_Product();
		$this->_error = $productObj->validate($this->_product); 
	 }
	 
	
	 
	 
	
	 
	 /*
	 *Update product
	 *
	 */
	 
	 private function update(){
		$Obj = new Merchant_Model_Product();		 
		if($Obj->changeProductStatus($this->_productId,$this->_status)){
			$this->writeLog();
			return true;
		}
		return false;
	 }
	 
	private function setProduct(){
		if(!isset($this->_product)||$this->_product['product_id']!=$this->_productId ){	


			$productModel= new Merchant_Model_Product();		 
		    $dbAdapter = $productModel->getDbTable()->getAdapter();
		    $dbAdapter->beginTransaction();
			$product = Merchant_Model_Product::getInstance()->getProduct($this->_productId,$this->_customerId);
			$dbAdapter->commit();			
			if($product instanceof Zend_Db_Table_Row){
				$this->_product	= $product->toArray();
				
			}else{
				 $this->_error = 'no permissions to proccessing this product!';
				 return $this->_error;
			}
			
		}else{
			$this->_error = 'product id can not be null!';
			return $this->_error;	
		}
	 }
	 
	public function setProductBySku($productSku=null,$customerId=null){
		if($productSku!=null && $customerId!=null){	
			$this->setCustomer($customerId);
			$productModel= new Merchant_Model_Product();		 
		    $dbAdapter = $productModel->getDbTable()->getAdapter();
		    $dbAdapter->beginTransaction();
			$product = Merchant_Model_Product::getInstance()->getProductBySku($productSku,$this->_customerId);
			$dbAdapter->commit();			
			if($product instanceof Zend_Db_Table_Row){
				$this->_product	= $product->toArray();
			}else{
				 $this->_error = 'no permissions to proccessing this product!';
				 return $this->_error;
			}
			$this->_productId=$this->_product['product_id'];
		}else{
			$this->_error = 'productSku can not be null!';
			return $this->_error;
		}
	 }
	 
	 public function writeLog(){
		$logRow = array(
			"product_id" => $this->_productId,
			"ph_type" => "1",
			"user_id" => '-1',
			"customer_id" => $this->_customerId,
			"ph_statu_pre" => $this->_product['product_status'],
			"ph_statu_now" => $this->_status,
			"ph_note" => $this->_logNote,
			"ph_add_time"=>date('Y-m-d H:i:s'),
			"ph_ip" => $_SERVER['REMOTE_ADDR'],
		);
		$dbTable = Merchant_Model_ProductHistory::getInstance()->getDbTable();
		 
		$dbTable->insert($logRow);
		 
	 }
}